package core.view
{
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.component.TextView;
	import org.aisy.display.USprite;

	public class DownLoadItem extends USprite
	{
		protected var NAME:String;
		protected var index:uint;
		protected var itemInfo:*;
		
		protected var _toAlpha:*;
		
		public function DownLoadItem(name:String, index:uint, data:*)
		{
			NAME = name;
			this.index = index;
			itemInfo = data;
			init();
			name = null;
			data = null;
		}
		
		protected function init():void
		{
			var _tv:TextView = new TextView();
			_tv.setText("Download_" + index);
			_tv.setColor(0x009bdf);
			_tv.dynamic = {"mouseEnabled": false};
			_tv.buttonMode = true;
			addChild(_tv);
			
			_tv.addEventListener(MouseEvent.CLICK, __mouseHandler);
			
			var a:Class = Ais.IMain.Swf.getClass("toalpha", "core.view.ToAlpha") as Class;
			if (a) {
				_toAlpha = new a(this, null, [[0.5, 0.9, 0.3]]);
				a = null;
			}
			_tv = null;
		}
		
		protected function __mouseHandler(e:MouseEvent):void
		{
			var isH:Boolean;
			var _byteArr:Array = Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().getByteArr();
			if (_byteArr[itemInfo]) isH = true;
			if (isH) {
				var b:ByteArray;
				if (e.ctrlKey === true) {
					var _fileHeadArr:Array = Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().getFileHeadArr();
					b = new ByteArray();
					b.writeBytes(_fileHeadArr[itemInfo]);
					b.writeBytes(_byteArr[itemInfo]);
					_fileHeadArr = null;
				}
				else {
					b = _byteArr[itemInfo];
				}
				var _f:FileReference = new FileReference();
				_f.save(b, "file_" + getTimer());
				_f = null;
				b = null;
			}
			else {
				var _tv:TextView = new TextView();
				_tv.setText("该文件可能已变更...");
				_tv.setColor(0x009bdf);
				TEvent.trigger("UP_WINDOW_AIS", "SHOW", [_tv, getTimer() + "" + Math.random(), false, true]);
				_tv = null;
			}
			_byteArr = null;
			e = null;
		}
		
		override public function clear():void
		{
			if (_toAlpha) _toAlpha.clear();
			_toAlpha = null;
			NAME = null;
			itemInfo = null;
			super.clear();
		}
		
	}
}